package com.xiangyang.service.impl;

import cn.hutool.system.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyang.entity.LoginUser;
import com.xiangyang.entity.User;
import com.xiangyang.entity.dto.Result;
import com.xiangyang.entity.vo.UserInfoPage;
import com.xiangyang.mapper.IUserInfoMapper;
import com.xiangyang.mapper.IUserMapper;
import com.xiangyang.service.IUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl extends ServiceImpl<IUserInfoMapper, User> implements IUserInfoService {

    private static final Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);
    @Autowired
    private IUserInfoMapper userMapper;

    @Override
    public Result getUserInfo(User user){
        return Result.ok(userMapper.selectList(null));
    }


    @Override
    public Result updateUserInfo(User user) {
        if (user == null) {
            return Result.error("用户信息不能为空");
        }
        if (user.getUserId() == null) {
            return Result.error("用户ID不能为空");
        }
        if(user.getUserType()==1){
            return Result.error("超级管理员的权限不允许修改，若需要修改请联系软件开发人员。");

        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        user.setUpdateBy(loginUser.getUserDetailInfo().getUserName());

        try {
            int rowsUpdated = userMapper.updateById(user);
            if (rowsUpdated <= 0) {
                return Result.error("更新用户信息失败");
            }
            return Result.ok();
        } catch (Exception e) {
            logger.error("Error updating user info", e);
            return Result.error("更新用户信息失败");
        }
    }

    @Override
    public Result deleteUserInfo(Long userId) {
        try {
            int rowsDeleted = userMapper.deleteById(userId);
            if (rowsDeleted <= 0) {
                return Result.error("删除用户信息失败");
            }
            return Result.ok();
        } catch (Exception e) {
            logger.error("Error deleting user info", e);
            return Result.error("删除用户信息失败");
        }
    }

    @Override
    public Result selectUserInfo(UserInfoPage userInfoPage) {
        Page<User> Page = new Page<>(userInfoPage.getPage(), userInfoPage.getSize());
        LambdaQueryWrapper<User> queryWrapper = new  LambdaQueryWrapper<>();
        queryWrapper.like(User::getUserName, userInfoPage.getUserName());
        queryWrapper.like(User::getUserPhone, userInfoPage.getUserPhone());
        return Result.ok(userMapper.selectPage(Page, queryWrapper));
    }


}